package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author ChenChangYu
 * @Date 2022 09 23 10 58
 **/
@Component
@Slf4j
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class VendoutResultHandler implements MsgHandler {

    @Autowired
    private OrderService orderService;

    /**
     * 接收售货机出货结果
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        if (StringUtils.isBlank(jsonMsg)){
            throw new LogicException("接收消息失败");
        }
        VendoutResultContract resultContract = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        if (resultContract == null){
            return;
        }
        //出货成功
        if (resultContract.isSuccess()){
                orderService.vendoutSuccess(resultContract);
        }else {
            //出货失败
            orderService.vendoutFail(resultContract);
        }

    }
}






























